package org.rfs.ipodencoder;

import java.text.DecimalFormat;
import java.util.HashMap;
import com.trolltech.qt.core.QTime;
import com.trolltech.qt.core.Qt;
import com.trolltech.qt.core.Qt.WindowFlags;
import com.trolltech.qt.gui.*;

public class IPodEncoderFileInfoView extends QDialog {

	IPodEncoderFileInfo ui = new IPodEncoderFileInfo();
	DecimalFormat df = new DecimalFormat("#.00");
	Signal4<String, String, String, String> applyEncodingSettings = new Signal4<String, String, String, String>();
	
	int vWidth, vHeight, tWidth, tHeight, aBitrate, ovWidth, ovHeight, oaBitrate, taBitrate;
	long vBitrate, ovBitrate, tvBitrate;
	double dFPS, vLength, tFilesize, dAspect;
	boolean bUpdateValues = false;

	public IPodEncoderFileInfoView() {
		ui.setupUi(this);
	}

	public IPodEncoderFileInfoView(QWidget parent, String fileName,
			HashMap<String, HashMap<String, String>> fileInfos) {
		super(parent,new WindowFlags(Qt.WindowType.WindowTitleHint));
		ui.setupUi(this);
		
		HashMap<String, String> currentInfo = fileInfos.get(fileName);
		ui.lineEdit_filename.setText(fileName);
		ui.lineEdit_demuxer.setText(currentInfo.get("DEMUXER"));
		vLength = Double.parseDouble(currentInfo.get("LENGTH"));
		ui.lineEdit_length.setText(getHMSTime(vLength)); // Seconds

		ui.lineEdit_video_format.setText(currentInfo.get("VIDEO_FORMAT"));
		vBitrate = Long.parseLong(currentInfo.get("VIDEO_BITRATE"));
		ovBitrate = vBitrate;
		tvBitrate = vBitrate / 1000;
		ui.lineEdit_video_bitrate.setText(String.valueOf(vBitrate / 1000));

		vWidth = Integer.parseInt(currentInfo.get("VIDEO_WIDTH"));
		vHeight = Integer.parseInt(currentInfo.get("VIDEO_HEIGHT"));
		ui.lineEdit_video_dimension.setText(currentInfo.get("VIDEO_WIDTH") + "x" + currentInfo.get("VIDEO_HEIGHT"));
		
		dFPS = Double.parseDouble(currentInfo.get("VIDEO_FPS"));
		ui.lineEdit_video_fps.setText(currentInfo.get("VIDEO_FPS"));
		ui.lineEdit_video_aspect.setText(currentInfo.get("VIDEO_ASPECT"));
		ui.lineEdit_video_codec.setText(currentInfo.get("VIDEO_CODEC"));
		
		ui.lineEdit_audio_format.setText(currentInfo.get("AUDIO_FORMAT"));
		aBitrate = Integer.parseInt(currentInfo.get("AUDIO_BITRATE"));
		oaBitrate = aBitrate;
		taBitrate = aBitrate / 1000;
		ui.lineEdit_audio_bitrate.setText(String.valueOf(aBitrate / 1000));
		ui.lineEdit_audio_rate.setText(currentInfo.get("AUDIO_RATE"));
		ui.lineEdit_audio_channels.setText(currentInfo.get("AUDIO_NCH"));
		ui.lineEdit_audio_codec.setText(currentInfo.get("AUDIO_CODEC"));
		
		ui.lineEdit_target_filesize.setText(String.valueOf(Math.round(calculate("filesize"))));
		ui.lineEdit_target_video_bitrate.setText(String.valueOf(vBitrate / 1000));
		ui.lineEdit_target_audio_bitrate.setText(String.valueOf(aBitrate / 1000));
		
		ui.lineEdit_target_width.setText(String.valueOf(vWidth));
		ui.lineEdit_target_height.setText(String.valueOf(vHeight));
		dAspect = ((double)vWidth / (double)vHeight);
		bUpdateValues = true;
	}
	
	private long calculate(String targetValue) {
		if (targetValue.equals("filesize")) {
			return Math.round((tvBitrate + taBitrate) * vLength / 8.00
					/ 1024.00);
		} else if (targetValue.equals("videobitrate")) {
			return Math.round(((double) tFilesize * 8192.00 / vLength)
					- (double) taBitrate);
		}
		return -1;
	}
	
	private int getHeightBy16(int width) {
		return (int)(Math.round(width / dAspect / 16) * 16);
	}
	
	private String getHMSTime(double seconds) {
		long secLong = (long)(seconds * 1000);
		long secTotal = secLong / 1000;
		int sec = (int) (secTotal % 60);
		long minTotal = secTotal / 60;
		int min = (int) (minTotal % 60);
		int hrTotal = (int) (minTotal / 60);
		QTime t = new QTime();
		t.setHMS(hrTotal, min, sec);
		return t.toString("hh:mm:ss");
	}
	
	public void on_lineEdit_target_width_textChanged() {
		try {
			tWidth = Integer.parseInt(ui.lineEdit_target_width.text());
			//tHeight = Integer.parseInt(ui.lineEdit_target_height.text());
			if (ui.checkBox_round.isChecked())
				ui.lineEdit_target_height.setText(String.valueOf(getHeightBy16(tWidth)));
			else
				ui.lineEdit_target_height.setText(String.valueOf(Math.round((double)tWidth / dAspect)));
		} catch (Exception ex) {}		
	}
	
	public void on_lineEdit_target_height_textChanged() {
		try {
			tWidth = Integer.parseInt(ui.lineEdit_target_width.text());
			tHeight = Integer.parseInt(ui.lineEdit_target_height.text());
		} catch (Exception ex) {}
	}
	
	public void on_checkBox_round_clicked() {
		on_lineEdit_target_width_textChanged();
	}
	
	public void on_lineEdit_target_video_bitrate_textChanged() {
		if (bUpdateValues) {
			try {
				tvBitrate = Long.parseLong(ui.lineEdit_target_video_bitrate.text());
				taBitrate = Integer.parseInt(ui.lineEdit_target_audio_bitrate.text());
				bUpdateValues = false;
				ui.lineEdit_target_filesize.setText(String.valueOf(calculate("filesize")));
				bUpdateValues = true;
			} catch (Exception ex) {
				// TODO: print a little info string beneath the "target"
				// controls
			}
		}
	}
	
	public void on_lineEdit_target_audio_bitrate_textChanged() {
		on_lineEdit_target_video_bitrate_textChanged();
	}
	
	public void on_lineEdit_target_filesize_textChanged() {
		if (bUpdateValues) {
			try {
				tFilesize = Double.parseDouble(ui.lineEdit_target_filesize.text());
				taBitrate = Integer.parseInt(ui.lineEdit_target_audio_bitrate.text());
				bUpdateValues = false;
				ui.lineEdit_target_video_bitrate.setText(String.valueOf(calculate("videobitrate")));
				bUpdateValues = true;
			} catch (Exception ex) {
				// TODO: print a little info string beneath the "target"
				// controls
			}
		}
	}
	
	public void on_pushButton_apply_clicked() {
		applyEncodingSettings.emit( ui.lineEdit_target_audio_bitrate.text(), 
									ui.lineEdit_target_video_bitrate.text(),
									ui.lineEdit_video_fps.text(),
									ui.lineEdit_target_width.text() + "x" + ui.lineEdit_target_height.text());
	}
}
